
local gouxian = fk.CreateSkill{
  name = "xiaobai__gouxian"
}

Fk:loadTranslationTable{
  ["xiaobai__gouxian"] = "构弦",
  [":xiaobai__gouxian"] = "出牌阶段结束时，若场上有三名角色的手牌数能构成三角形，你可以将手牌数调整为4；若能构成等腰三角形，"..
  "你可以令其中两名角色交换手牌。",
  ["#xiaobai__gouxian-invoke"] = "构弦：是否将手牌数调整为4？",
  ["#xiaobai__gouxian-choose"] = "构弦：调整后你可以令手牌数构成等腰三角形角色中的两名角色交换手牌",
  ["xiaobai__gouxian_active"] = "构弦",

  ["$xiaobai__gouxian1"] = "周公问天地之数，吾辈学者求之不倦。",
  ["$xiaobai__gouxian2"] = "迭相规矩，共为返覆，互与通分，各有所得。",
}

local U = require "packages.utility.utility"

gouxian:addEffect(fk.EventPhaseEnd,{
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(self) and player.phase == Player.Play and #player.room.alive_players > 2 then
      local nums = {}
      for _, p in ipairs(player.room.alive_players) do
        if not p:isKongcheng() then
          table.insert(nums, p:getHandcardNum())
        end
      end
      if #nums < 3 then return end
      table.sort(nums, function (a, b)
        return a > b
      end)
      for i = 1, #nums - 2, 1 do
        if nums[i] < nums[i + 1] + nums[i + 2] then
          return true
        end
      end
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local nums = {}
    for _, p in ipairs(player.room.alive_players) do
      if not p:isKongcheng() then
        table.insert(nums, p:getHandcardNum())
      end
    end
    if #nums < 3 then return end
    table.sort(nums, function (a, b)
      return a > b
    end)
    local yes, results = false, {}
    for i = 1, #nums - 2, 1 do
      for j = i + 1, #nums - 1, 1 do
        for k = j + 1, #nums, 1 do
          if nums[i] < nums[j] + nums[k] then
            yes = true
            if nums[j] == nums[i] or nums[j] == nums[k] then
              table.insert(results, {nums[i], nums[j], nums[k]})
            end
          end
        end
      end
    end
    local success, dat = nil,nil
    if yes and player:getHandcardNum() ~= 4 and room:askToSkillInvoke(player, {skill_name = gouxian.name, prompt = "#xiaobai__gouxian-invoke"}) then
      room:setPlayerMark(player, "xiaobai__gouxian-tmp", results)
      success, dat = room:askToUseActiveSkill(player, {
        skill_name = "xiaobai__gouxian_active",
        prompt = "#xiaobai__gouxian-choose",
        cancelable = true,
        no_indicate = false,
      })
      room:setPlayerMark(player, "xiaobai__gouxian-tmp", 0)
      local n = player:getHandcardNum() - 4
      if n < 0 then
        player:drawCards(-n, gouxian.name)
        if player.dead then return end
      else
        room:askToDiscard(player, {
          min_num = n,
          max_num = n,
          include_equip = false,
          cancelable = false,
          skill_name = gouxian.name
        })
      end
    end
    if #results > 0 and not player.dead then
      if success and dat then
        room:delay(400)
        room:swapAllCards(player, dat.targets, gouxian.name)
      end
    end
  end,
})

local gouxian_active = fk.CreateSkill{
  name = "xiaobai__gouxian_active",
}

gouxian_active:addEffect("active",{
  card_num = 0,
  target_num = 2,
  card_filter = Util.FalseFunc,
  target_filter = function (self, player, to_select, selected)
    local results = Self:getMark("xiaobai__gouxian-tmp")
    local n =to_select:getHandcardNum()
    if #selected == 0 then
      return table.find(results, function (dat)
        return table.contains(dat, n)
      end)
    elseif #selected == 1 then
      local n2 = selected[1]:getHandcardNum()
      for _, dat in ipairs(results) do
        if table.contains(dat, n) and table.contains(dat, n2) then
          if n ~= n2 then
            return true
          elseif #table.filter(dat, function (i)
            return i == n
          end) > 1 then
            return true
          end
        end
      end
    end
  end,
})

local skels = {}
table.insert(skels, gouxian)
table.insert(skels, gouxian_active)

return skels